@file:Suppress("unused")

package com.example.demo.model

import com.example.demo.handler.*
import xyz.erupt.annotation.*
import xyz.erupt.annotation.expr.*
import xyz.erupt.annotation.sub_erupt.*
import xyz.erupt.annotation.sub_field.*
import xyz.erupt.annotation.sub_field.sub_edit.*
import xyz.erupt.jpa.model.*
import xyz.erupt.upms.handler.*
import javax.persistence.*

@Erupt(
    name = "复杂示例",
    rowOperation = [
        RowOperation(
            operationHandler = OperationHandlerImpl2::class,
            mode = RowOperation.Mode.SINGLE,
            title = "单行操作"
        ),
        RowOperation(
            title = "多行操作",
            show = ExprBool(exprHandler = ViaMenuValueCtrl::class, params = ["ComplexBtn"]),
            icon = "fa fa-check-square",
            operationHandler = OperationHandlerImpl::class,
        ),
        RowOperation(
            eruptClass = ComplexOperator::class,
            operationHandler = OperationHandlerImpl::class,
            mode = RowOperation.Mode.BUTTON,
            tip = "不依赖任何数据即可执行",
            title = "按钮操作",
            icon = "fa fa-google-wallet",
        ),
        RowOperation(
            type = RowOperation.Type.TPL,
            mode = RowOperation.Mode.MULTI,
            tpl = Tpl(path = "/tpl/operation.ftl", engine = Tpl.Engine.FreeMarker),
            title = "自定义模板",
            icon = "fa fa-pagelines"
        ),
    ],
    linkTree = LinkTree(field = "tree"),
)
@Table(name = "demo_complex")
@Entity
class Complex : BaseModel() {

    @EruptField(
        views = [View(title = "动态下拉列表")],
        edit = Edit(
            search = Search(),
            title = "通过SQL获取下拉列表",
            type = EditType.CHOICE,
            desc = "动态下拉列表",
            choiceType = ChoiceType(
                fetchHandler = [SqlChoiceFetchHandler::class],
                fetchHandlerParams = ["select id,name from e_upms_menu"],
            ),
        ),
    )
    var choice: String? = null

    @EruptField(
        views = [View(title = "字典值")],
        edit = Edit(
            search = Search(),
            title = "字典值",
            type = EditType.CHOICE,
            desc = "动态获取字典项的值",
            choiceType = ChoiceType(
                fetchHandler = [DictChoiceFetchHandler::class],
                fetchHandlerParams = ["system"],
            ),
        ),
    )
    var fromDict: Long? = null

    @ManyToOne
    @EruptField(
        edit = Edit(
            title = "多对一树",
            search = Search(),
            type = EditType.REFERENCE_TREE,
            referenceTreeType = ReferenceTreeType(
                id = "id",
                label = "name",
                pid = "parent.id",
            ),
        ),
    )
    var tree: TreeView? = null

    @ManyToOne
    @JoinColumn
    @EruptField(
        views = [View(title = "多对一表格", column = "title")],
        edit = Edit(
            title = "多对一表格",
            type = EditType.REFERENCE_TABLE,
            search = Search(),
            referenceTableType = ReferenceTableType(label = "title"),
        ),
    )
    var article: Article? = null

    @Lob
    @EruptField(
        views = [View(title = "python代码", type = ViewType.CODE)],
        edit = Edit(
            title = "python代码编辑器",
            type = EditType.CODE_EDITOR,
            codeEditType = CodeEditorType(language = "python"),
        ),
    )
    var code: String? = null

    @OneToMany(cascade = [CascadeType.ALL])
    @JoinColumn(name = "complex_id")
    @EruptField(edit = Edit(title = "一对多新增", type = EditType.TAB_TABLE_ADD))
    var complexTab: Set<ComplexTab>? = null

    @ManyToMany
    @JoinTable(
        name = "e_complex_article",
        joinColumns = [JoinColumn(name = "complex_id", referencedColumnName = "id")],
        inverseJoinColumns = [JoinColumn(name = "article_id", referencedColumnName = "id")],
    )
    @EruptField(
        edit = Edit(
            title = "关联多篇文章",
            type = EditType.TAB_TABLE_REFER,
            referenceTableType = ReferenceTableType(label = "title"),
        ),
    )
    var articleTab: Set<Article>? = null

}